import 'package:flutter/material.dart';
import '../service/service_method.dart';
import '../model/cartmodel.dart';
import 'dart:convert';

class CartProvides with ChangeNotifier{
  Cartmodel cartdata = null;
  // GoodList cartlist = null;
  int nums = 10;

  getCartInfo(token) async{
    print('加载购物车数据中=======================');
    var formData = {
      "token":token
    };
    await DioUtil.request("getOrderCartDat",formData: formData).then((val){
       var responseDatas = val;
      print(responseDatas);
      cartdata = Cartmodel.fromJson(responseDatas);
      print(cartdata);
      print('购物车数据获取成功');
      notifyListeners();
    });
  }

  addCart(item,token) async{
    cartdata.data.goodList[item].cartNum++;
    print(token);
    var formDataa = {
      "token":token,
      "cart_id" :cartdata.data.goodList[item].cartId,
      "cart_num":cartdata.data.goodList[item].cartNum,
      "is_checked":cartdata.data.goodList[item].isChecked
    };
    print(formDataa);
    await DioUtil.request('editOrderCart',formData: formDataa).then((val){
      print(val);
      print(formDataa);
      print('购物车编辑成功');
    });
    
    notifyListeners();
    print('加法');
  }
  removeCart(item,token) async{
     
    if (cartdata.data.goodList[item].cartNum <= 0) {
      print('已经最小啦'); 
    }else{
      cartdata.data.goodList[item].cartNum--;
      print('减法');
          var formDataa = {
      "token":token,
      "cart_id" :cartdata.data.goodList[item].cartId,
      "cart_num":cartdata.data.goodList[item].cartNum,
      "is_checked":cartdata.data.goodList[item].isChecked
    };
    print(formDataa);
    await DioUtil.request('editOrderCart',formData: formDataa).then((val){
      print(val);
      print(formDataa);
      print('购物车编辑成功');
    });
       
    }
    notifyListeners();
  
     
  }

  getselection(index,token) async{
    if (cartdata.data.goodList[index].isChecked == 1) {
      cartdata.data.goodList[index].isChecked = 0;
    }else{
      cartdata.data.goodList[index].isChecked = 1;
    }
                  var formDataa = {
      "token":token,
      "cart_id" :cartdata.data.goodList[index].cartId,
      "cart_num":cartdata.data.goodList[index].cartNum,
      "is_checked":cartdata.data.goodList[index].isChecked
    };
    print(formDataa);
    await DioUtil.request('editOrderCart',formData: formDataa).then((val){
      print(val);
      print(formDataa);
      print('购物车编辑成功');
    });
    notifyListeners();
  }

}